package com.etc.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Users;
import com.etc.service.UsersService;

@Controller
public class UsersController {

	@Resource(name = "usersService")
	private UsersService usersService;

	@RequestMapping(value = "addUser")
	public String addUser(Users users) {
		Users u = usersService.adduser(users);
		System.out.println(u);
		return "response";
	}

	// 方法一： 从控制器传递参数给视图层采用request方法
	@RequestMapping(value = "test5")
	public String test5(HttpServletRequest request) {
		System.out.println("test5");
		request.setAttribute("key", "value");
		return "test5";
	}

	// 方法二：从控制器传递参数给视图采用ModelAndView
	@RequestMapping(value = "test6")
	public ModelAndView test6() {
		// 创建ModelAndView的对象
		ModelAndView mv01 = new ModelAndView();
		mv01.setViewName("test6");
		mv01.addObject("key6", "value6");

		System.out.println("test6");
		return mv01;
	}

	@RequestMapping(value = "test7")
	public ModelAndView test7() {
		List<String> list = new ArrayList<>();
		list.add("张三");
		list.add("李四");

		// 创建一个ModelAndView对象
		ModelAndView mv02 = new ModelAndView("test7");
		mv02.addObject("key7", list);

		System.out.println("test7");

		return mv02;
	}

	@RequestMapping(value = "test8")
	public ModelAndView test8() {
		List<String> list = new ArrayList<>();
		list.add("张三");
		list.add("李四");

		Map<Integer, String> map = new HashMap();
		map.put(1, "巴黎圣母院失火了");
		map.put(2, "今日新闻");
		map.put(3, "视觉中国说");

		// 创建一个ModelAndView对象
		ModelAndView mv03 = new ModelAndView("test8");
		mv03.addObject("key8", list);

		mv03.addObject("key9", map);

		System.out.println("test8");

		return mv03;
	}

}
